const asyncPost = (url, param) => {
	if(url.indexOf('?')!=-1)
	{
		url = url+`&t=${(new Date()).getTime()}`
	}else{
		url = url+`?t=${(new Date()).getTime()}`
	}
	return new Promise((resolve, reject) => {
		wx.request({
			url: url,
			method: 'POST',
			header: {
				'content-type': 'application/x-www-form-urlencoded' // 默认值
			},
			data: param,
			success(res) {
				//console.log(res);
				resolve(res);
			},
			fail(err) {
				reject(err);
			}
		});
	})
}

const asyncGet=(url,param)=>{
	if(url.indexOf('?')!=-1)
	{
		url = url+`&t=${(new Date()).getTime()}`
	}else{
		url = url+`?t=${(new Date()).getTime()}`
	}
	return new Promise((resolve, reject) => {
		wx.request({
			url: url,
			method: 'GET',
			data: param,
			success(res) {
				//console.log(res);
				resolve(res);
			},
			fail(err) {
				reject(err);
			}
		});
	});
}

const asyncOAuthGet=(url,token,param)=>{
	if(url.indexOf('?')!=-1)
	{
		url = url+`&t=${(new Date()).getTime()}`
	}else{
		url = url+`?t=${(new Date()).getTime()}`
	}
	return new Promise((resolve, reject) => {
		//debugger;
		wx.request({
			url: url,
			method: 'GET',
			header: {
				'Authorization':'Bearer '+token // 默认值
			},
			data: param,
			success(res) {
				//console.log(res);
				resolve(res);
			},
			fail(err) {
				reject(err);
			}
		});
	});
}

const callbackRequst = (url = '', header = {}, method = 'GET', params = {}, success, error) => {
	if(url.indexOf('?')!=-1)
	{
		url = url+`&t=${(new Date()).getTime()}`
	}else{
		url = url+`?t=${(new Date()).getTime()}`
	}
    wx.request({
        url: url,
        method: method,
        header: header,
        data: params,
        success(res) {
			if(res.statusCode==200)
			{
				success(res);
			}else{
				error(res);
			}
        },
        fail(err) {
            error(err);
        }
    });
}


const promiseRequst=(url = '', header = {}, method = 'GET', params = {})=>{
	if(url.indexOf('?')!=-1)
	{
		url = url+`&t=${(new Date()).getTime()}`
	}else{
		url = url+`?t=${(new Date()).getTime()}`
	}
	return new Promise((resolve, reject) => {
		wx.request({
			url: url,
			method: method,
			header: header,
			data: params,
			dataType:'json',
			success(res) {
				//debugger;
				if(res.statusCode==200)
				{
					resolve(res);
				}else{
					reject(res);
				}
			},
			fail(err) {
				//debugger;
				reject(err);
			}
		});
	});
}

module.exports={
	asyncPost,
	asyncGet,
	asyncOAuthGet,
	callbackRequst,
	promiseRequst
}
